package checker.sdchecker;

import java.lang.reflect.InvocationTargetException;

import javax.swing.SwingUtilities;

public abstract class EDTTask implements Runnable {

    @Override
    public final void run() {
        beforeEDTCall();
        try {
            SwingUtilities.invokeAndWait(new Runnable() {

                @Override
                public void run() {
                    runOnEDT();
                }});
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } catch (InvocationTargetException e) {
            onException(e.getTargetException());
        } finally {
            afterEDTCall();
        }
    }

    protected void beforeEDTCall() {
    }

    protected void afterEDTCall() {
    }

    protected abstract void runOnEDT();

    protected void onException(Throwable throwable) {
        throwable.printStackTrace();
    }
    
}
